package com.just.spring4.ch1.aop;

import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect    //声明一个切面
@Component //让切面成为spring管理的bean
public class LogAspect {
//    @Pointcut("@annotation(com.just.spring4.ch1.aop.Action)")  //声明切点
//    public void annotationPointCut(){}

    @After("@annotation(action)")   //通过@after注解声明一个建言，并使用@Pointcut定义的切点
    public void after(JoinPoint joinPoint,Action action){
//        MethodSignature signature= (MethodSignature) joinPoint.getSignature();
//        Method method=signature.getMethod();
        //Action action=method.getAnnotation(Action.class);
        System.out.println("注解式拦截,"+action.name());
    }

    @Before("execution(* com.just.spring4.ch1.aop.DemoMethodService.*(..))") //直接使用拦截规则作为参数
    public void before(JoinPoint joinPoint){
        MethodSignature signature= (MethodSignature) joinPoint.getSignature();
        Method method=signature.getMethod();
        System.out.println("方法规则拦截，"+method.getName());
    }
}
